// 创建axios实例
import {ElMessage} from "element-plus";
import axios from 'axios';

const service = axios.create({
    baseURL: 'http://localhost:8085/api',
    timeout: 10000
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        // Spring Boot直接返回数据，不需要判断code
        return response.data;
    },
    error => {
        ElMessage.error(error.response?.data?.message || '请求失败');
        return Promise.reject(error);
    }
);

// 导出全局的 axios 实例
export default service;